package lj.jdbctest.jdbc;

import java.sql.*;

/**
 * @author lj
 * @Description: 快速入门
 * @date 2020-11-23 13:02
 * @QQ 851477108
 */


public class jdbc5 {
    public static void main(String[] args) throws SQLException {
        Statement statement = null;
        Connection conn = null;
        ResultSet rs = null;
        try {
            //1. 导入驱动jar包
            //2.注册驱动
//        Class.forName("com.mysql.jdbc.Driver");
            //3.获取数据库连接对象
            conn = DriverManager.getConnection("jdbc:mysql:///db3","root","root");
            //4.定义sql语句
            String sql = "select * from account";
            //5.获取执行sql的对象 Statement
            statement = conn.createStatement();
            //6.执行sql
            rs = statement.executeQuery(sql);
            //6.1游标向下移动一行
            rs.next();
            //6.2判断是否有数据

            //6.3获取数据
            int id = rs.getInt(1);
            String name = rs.getString("name");
            double flag = rs.getDouble(3);
            System.out.println(id+"-"+name+"-"+flag);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //8.释放资源
            if (rs!=null){
                rs.close();
            }
            if (statement!=null){
                statement.close();
            }
            if (conn!=null){
                conn.close();
            }
        }
    }
}
